package com.ushine.web.component.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ushine.web.component.exception.BaseErrorInfoInterface;
import com.ushine.web.component.page.PageResultVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;

import java.io.Serializable;

/**
 * @Author yls
 * @Description HTTP结果封装
 * @Date 2020/4/5 9:41
 **/
@ApiModel(description = "HTTP结果封装")
@Data
@Slf4j
public class HttpResponseVO<T extends Object> implements Serializable {

    @ApiModelProperty("状态")
    private Integer code;

    @ApiModelProperty("msg信息")
    private String msg;

    @ApiModelProperty("返回数据")
    private T data;

    public HttpResponseVO() {

    }

    /**
     * 功能描述:
     * 〈全局异常捕获并返回〉
     *
     * @param baseErrorInfoInterface 基础错误类
     * @return : com.authority.manager.base.http.HttpResponseVO
     * @author : yls
     * @date : 2020/10/20 10:50
     */
    public HttpResponseVO<Object> error(BaseErrorInfoInterface baseErrorInfoInterface) {
        return error(baseErrorInfoInterface.getErrorCode(), baseErrorInfoInterface.getErrorMsg());
    }


    /**
     * 功能描述:
     * 〈返回500错误信息〉
     *
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:38
     */
    public HttpResponseVO<Object> error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    /**
     * 功能描述:
     * 〈操作异常〉
     *
     * @param msg 异常信息
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:39
     */
    public HttpResponseVO<Object> error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    /**
     * 功能描述:
     * 〈自定义错误异常〉
     *
     * @param code 错误代码
     * @param msg  异常信息
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:39
     */
    public HttpResponseVO<Object> error(int code, String msg) {
        HttpResponseVO<Object> r = new HttpResponseVO<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    /**
     * 功能描述:
     * 〈成功返回信息〉
     *
     * @param msg 响应消息
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:40
     */
    public HttpResponseVO<Object> ok(String msg) {
        HttpResponseVO<Object> r = new HttpResponseVO<>();
        r.setCode(HttpStatus.SC_OK);
        r.setMsg(msg);
        return r;
    }

    /**
     * 功能描述:
     * 〈成功返回〉
     *
     * @param data 响应实体
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:40
     */
    public HttpResponseVO<T> errorValid(T data) {
        HttpResponseVO<T> r = new HttpResponseVO<>();
        r.setMsg("数据校验未通过！！！");
        r.setCode(HttpStatus.SC_BAD_REQUEST);
        r.setData(data);
        return r;
    }

    /**
     * 功能描述:
     * 〈成功返回〉
     *
     * @param data 响应实体
     * @param msg  响应信息
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:40
     */
    public HttpResponseVO<T> ok(String msg, Object data) {
        this.setCode(HttpStatus.SC_OK);
        this.setMsg(msg);
        //判断是否为分页对象
        if (data instanceof Page) {
            Page<?> page = (Page) data;
            PageResultVO pageResult = new PageResultVO();
            pageResult.setContent(page.getContent());
            pageResult.setPageNum(page.getNumber() + 1);
            pageResult.setPageSize(page.getSize());
            pageResult.setTotalPages((long) page.getTotalPages());
            pageResult.setTotalSize(page.getTotalElements());
            this.setData((T) pageResult);
        } else if (data instanceof IPage) {
            IPage page = (IPage) data;
            PageResultVO pageResultVO = new PageResultVO();
            pageResultVO.setContent(page.getRecords());
            pageResultVO.setPageNum((int) page.getCurrent());
            pageResultVO.setPageSize((int) page.getSize());
            pageResultVO.setTotalPages(page.getPages());
            pageResultVO.setTotalSize(page.getTotal());
            this.setData((T) pageResultVO);
        } else {
            this.setData((T) data);
        }
        return this;
    }

    /**
     * 功能描述:
     * 〈成功返回〉
     *
     * @return : com.authority.manager.base.http.HttpResultVO
     * @author : yls
     * @date : 2020/10/14 9:41
     */
    public HttpResponseVO<T> ok() {
        return new HttpResponseVO<>();
    }

    public Boolean isSuccess() {
        return this.getCode() == HttpStatus.SC_OK;
    }

}
